﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Diagnostics;
using System.Xml.Linq;
using System.Windows.Markup;
using System.Windows.Threading;

using NakedCrome.lib;

namespace NakedCrome
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        App application = Application.Current as App;

        public MainWindow()
        {
            InitializeComponent();

            application.NakedChromeStarted += (e, sender) =>
            {
                rectangleWorking.Visibility = System.Windows.Visibility.Visible;
                textblockProcessStarted.Text = "初期状態のユーザープロファイルへ戻しました";
            };

            application.NakedChromeEnded += (e, sender) =>
            {
                textblockProcessStarted.Text = "現在利用しているユーザープロファイルへ戻しました";
                rectangleWorking.Visibility = System.Windows.Visibility.Hidden;
            };

            this.DataContext = this;
            this.FileVersionInfo =
                FileVersionInfo.GetVersionInfo(Environment.GetCommandLineArgs()[0]);
        }

        public FileVersionInfo FileVersionInfo
        {
            get { return (FileVersionInfo)GetValue(FileVersionInfoProperty); }
            set { SetValue(FileVersionInfoProperty, value); }
        }

        public static readonly DependencyProperty FileVersionInfoProperty =
            DependencyProperty.Register("FileVersionInfo", typeof(FileVersionInfo),
                typeof(MainWindow), new UIPropertyMetadata(null));

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Title = string.Format("{0} - {1}", textBlockTitle.Text, textBlockVersion.Text);

            DispatcherTimer timer = new DispatcherTimer();

            timer.Tick += (_1, _2) =>
            {
                Process[] processes = Process.GetProcesses();
                int count = processes.Count((p) => { return p.ProcessName == "chrome"; });
                textblockProcessState.Text = count == 0
                    ? "Google Chromeは起動していません"
                    : string.Format("Google Chromeのプロセスが{0}個見つかりました", count);
                buttonLaunchNakedChrome.IsEnabled = count == 0;
            };

            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Start();

            textblockProcessStarted.Text = "初期状態の Google Chrome を起動します";
        }

        private void buttonKillProcess_Click(object sender, RoutedEventArgs e)
        {
            application.KillChromeProcess((bool)checkBoxUseKill.IsChecked);
        }

        private void buttonLaunchNakedChrome_Click(object sender, RoutedEventArgs e)
        {
            application.LaunchNakedChrome();
        }

        private void buttonLaunchGoogleChrome_Click(object sender, RoutedEventArgs e)
        {
            application.LaunchNormalChrome();
        }

        private void buttonExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
